package whf.framework.web.query;

import java.lang.reflect.Constructor;
import java.util.Map;

import whf.framework.entity.PropertyTypeEnum;
import whf.framework.util.Utils;

public class FieldFactory {
	private static Map<PropertyTypeEnum, Class> REGISTERED_FIELDS = Utils.newHashMap();
	
	static {
		REGISTERED_FIELDS.put(PropertyTypeEnum.ENTITY, 			BOField.class);
		REGISTERED_FIELDS.put(PropertyTypeEnum.BOOLEAN, 	StringField.class);
		REGISTERED_FIELDS.put(PropertyTypeEnum.DATETIME, 	DateField.class);
		REGISTERED_FIELDS.put(PropertyTypeEnum.DICT, 		DictItemField.class);
		REGISTERED_FIELDS.put(PropertyTypeEnum.DOUBLE, 		NumberField.class);
		REGISTERED_FIELDS.put(PropertyTypeEnum.FLOAT, 		NumberField.class);
		REGISTERED_FIELDS.put(PropertyTypeEnum.LONG, 		NumberField.class);
		REGISTERED_FIELDS.put(PropertyTypeEnum.INTEGER, 	NumberField.class);
	}
	
	public static Field getField(String name, String[] values, PropertyTypeEnum type) {
		Class cls = REGISTERED_FIELDS.get(type);
		if(cls == null) cls = StringField.class;
		try{
			Constructor c = cls.getConstructor(new Class[]{String.class, String[].class});
			return (Field)c.newInstance(new Object[]{name, values});
		} catch(Exception e){
			return null;
		}
	}
}
